<template>
    <div>
        <h1>用药管理</h1>
        <div>
            医生姓名：<input type="text" v-model="queryinfo.UDoctor">
            科室名称: <select v-model="queryinfo.UDName">
                <option value="">请选择</option>
                <option :value="item.DName" v-for="item in list">{{ item.DName }}</option>
            </select>
            <input type="button" value="查询" class="btn btn-primary" @click="show">
            <input type="button" value="批量完成" class="btn btn-primary" @click="ups(1)">
            <input type="button" value="导出到Excel" class="btn btn-primary" @click="godata">
        </div>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <th><input type="checkbox" v-model="checked" @click="checkall"></th>
                    <th>药品名称</th>
                    <th>科室名称</th>
                    <th>用药日期</th>
                    <th>病人姓名</th>
                    <th>病房号码</th>
                    <th>用药状态</th>
                    <th>配药医生</th>
                    <th>操作</th>
                </tr>
                <tr v-for="item in data.list"> 
                    <th><input type="checkbox" v-model="ids" :value="item.UId" @change="checkone"></th>
                    <th>{{ item.UName }}</th>
                    <th>{{ item.UDName }}</th>
                    <th>{{ moment( item.UDate).format( "YYYY-MM-DD") }}</th>
                    <th>{{ item.UPerson }}</th>
                    <th>{{ item.UNum }}</th>
                    <th>{{ item.UState==1?"已完成":"未开始" }}</th>
                    <th>{{ item.UDoctor }}</th>
                    <th>
                        <input type="button" value="修改" class="btn btn-primary" @click="upd(item.UId)">
                        <input type="button" value="删除" class="btn btn-danger" @click="del(item.UId)"></th>
                </tr>
            </tbody>
        </table> 
        <div>
            共{{ data.totalCount }}条<span></span>
            共{{ data.totalPage }}页
            <span>{{ queryinfo.pageindex}}/{{ data.totalPage }}</span>
            <input type="button" value="首页" @click="gopage(1)">
            <input type="button" value="上一页" @click="gopage(queryinfo.pageindex-1)">
            <span v-for="item in data.totalPage" @click="gopage(item)">{{ item }}</span>
            <input type="button" value="下一页" @click="gopage(queryinfo.pageindex+1)">
            <input type="button" value="尾页" @click="gopage(data.totalPage)">
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref,reactive,onMounted } from 'vue';
import axios from 'axios';
import moment from 'moment';
import { useRoute,useRouter } from 'vue-router';
const route = useRoute()
const router = useRouter()

let queryinfo:any=reactive({
    pageindex:1,
    pagesize:2,
    UDoctor:"",
    UDName:""
})

let data:any=reactive({
    "list": [],
    "totalCount": 0,
    "totalPage": 0,
})

let list:any = ref([])

let checked=ref(false)

let ids=ref([])

onMounted(()=>{
    getdata() 
    getlist()
})


const upd=(UId:any)=>{
    router.push({
        path:'/useupdate',
        query:{
            UId:UId
        }
    })
}


const ups=(state:any)=>{
    axios({
        url:'/api/Use/UpdateSate',
        method:'put',
        params:{
            ids:ids.value.toString(),
            state:state
        }
    })
    .then((res)=>{
        console.log(res);
        if(res.data>0){
            alert("批量完成成功")
            checkall()
            checked.value=false
            getlist()
        }
        else{
            alert("批量完成失败")
            checked.value=false
            return
        }
    })
}

const checkall=()=>{
    checked.value=!checked.value
    if(checked.value==true){
        ids.value=data.list.map((item:any)=>{
            return item.UId
        })
    }
    else{
        ids.value=[]
    }
}


const checkone=()=>{
    if(ids.value.length==data.list.length){
        checked.value=true
    }
    else{
        checked.value=false
    }
}

const godata=()=>{
    location.href='https://localhost:7183/api/Use/ExportExcel'
}

const del=(UId:any)=>{
    let f = confirm("确定要删除吗？")
    if(f){
        axios({
            url:'/api/Use/DeleteUse',
            method:'delete',
            params:{
                UId:UId
            }
        })
        .then((res)=>{
            console.log(res);
            if(res.data>0){
            alert("删除成功")
            queryinfo.pageindex=1
            checked.value=false
            getlist()
        }
        else{
            alert("删除失败")
            queryinfo.pageindex=1
            return
        }
        })
    }
}

const show=()=>{
    queryinfo.pageindex=1
    checked.value=false
    getlist()
}

const gopage=(num:any)=>{
    if(num<1){
        queryinfo.pageindex=1
        return
    }
    if(num>data.totalPage){
        queryinfo.pageindex=data.totalPage
        return
    }
    queryinfo.pageindex=num
    checked.value=false
    getlist()
}

const getdata=()=>{
    axios({
        url:'/api/Depart/GetDepartment',
        method:'get',
    })
    .then((res)=>{
        console.log(res);
        list.value=res.data
    })
}

const getlist=()=>{
    axios({
        url:'/api/Use/GetUseModels',
        method:'get',
        params:queryinfo
    })
    .then((res)=>{
        console.log(res);
        Object.assign(data,res.data)
    })
}



</script>

<style scoped>
input{
    margin: 0 10px;
}
span{
    margin: 0 10px;
}
</style>